package control;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.admin.OptionalServiceVO;
import model.happy.UploadFileService;
import model.happy.UploadFileVO;
import model.album.HappyalbumVO;
import model.member.MemberVO;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UploadController extends MultiActionController{
	private String path;
	private String paht2;
	private UploadFileService uploadFileService;
	public UploadController(UploadFileService uploadFileService){
		this.uploadFileService=uploadFileService;
	}
	
	public void setPaht2(String paht2) {
		this.paht2 = paht2;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public ModelAndView fileUpload(HttpServletRequest request,HttpServletResponse response, UploadFileVO uvo){
		MultipartFile mfile=uvo.getUploadFile();
		if(mfile.isEmpty()==false){
			String fileName=mfile.getOriginalFilename();
			uvo.setFileName(fileName);
			File copyFile=new File(path+fileName);
			try {
				uploadFileService.insertHappyTime(uvo);
				mfile.transferTo(copyFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return new ModelAndView("happytime/upload_ok");
	}
	
	
	
	
//////////////////////////////////////////////////////////////////////////
	public ModelAndView albumUpload(HttpServletRequest request,HttpServletResponse response,HappyalbumVO happyalbumVO){
		MultipartFile mfile=happyalbumVO.getUploadFile();
		String optionalNoTest = request.getParameter("optionalNoTest");
		System.out.println(optionalNoTest);
		request.setAttribute("optionalNoTest", optionalNoTest);
		MemberVO mvo=(MemberVO) request.getSession().getAttribute("mvo");
		System.out.println(happyalbumVO.getOptionalService());
		System.out.println("세션맴버"+mvo);
		OptionalServiceVO serviceVO = null;
		try {
			serviceVO =  uploadFileService.getOptimalService(mvo);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		happyalbumVO.setOptionalService(serviceVO);
		if(mfile.isEmpty()==false){
			String fileName=mfile.getOriginalFilename();
			System.out.println(fileName);
			happyalbumVO.setFileName(fileName);
			System.out.println("문제1");
			String newfilename=mvo.getId()+"_"+System.currentTimeMillis()+"_"+fileName;
			System.out.println("문제2");
			happyalbumVO.setNewfilename(newfilename);
			System.out.println("문제3");
			File copyFile=new File(paht2+newfilename);
			System.out.println("문제4");
			try {
				System.out.println("문제5");
				mfile.transferTo(copyFile);
				System.out.println("문제6");
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("문제7");
		}
		try {
			uploadFileService.insertalbum(happyalbumVO);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("문제8");
		return new ModelAndView("insert_result.happyalbum");
	}
	

}
